package com.weipu.common.facade;
import com.weipu.common.constants.Constant;
import com.weipu.common.facade.exception.DxException;
import com.weipu.common.facade.impl.BaseServiceProviderImpl;
import com.weipu.common.facade.model.Parmars;
import com.weipu.common.facade.model.PushSettingsModel;
import com.weipu.common.okhttp.JsonUtils;
import org.json.JSONException;
import org.json.JSONObject;

/**
 * 物流信息服务类
 * @author Zenglw
 * @version [1.0,2017.6.20]
 */
public class TrackServiceProvider extends BaseServiceProviderImpl{

    /**
     * 保存用户的订阅配置信息
     * @param customerID 用户登陆时的ID
     * @param customerToken 用户登陆的Token
     * @param isPush 是否推送
     * @param pushType 推送类型
     * @param pushCircle 推送周期
     * @param cycleSetType 是否是用户自定义的周期
     */
    public boolean saveTrackConfig(Long customerID, String customerToken, boolean isPush, int pushType, int pushCircle, int cycleSetType) {
        Parmars parmars = new Parmars();
        parmars.setCustomerID(customerID);
        parmars.setToken(customerToken);
        parmars.setPush(isPush);
        parmars.setPushType(pushType);
        parmars.setPushCycle(pushCircle);
        parmars.setCycleSetType(cycleSetType);
        try {
            JSONObject jsonObject = requestHttpPostData(Constant.SETSHIPPINGMSGSETTING_URL, parmars);
            return jsonObject.getBoolean(Constant.JSON_ISSUCCESS);
        } catch (DxException e) {
            e.printStackTrace();
        } catch (JSONException e) {
            e.printStackTrace();
        }

        return false;
    }

    /**
     * 从服务器获取用户的订阅页面的配置信息
     * @param customerID 用户登陆时的ID
     * @param customerToken 用户登陆的Token
     */
    public PushSettingsModel.DataBean readTrackConfig(Long customerID, String customerToken) {
        Parmars parmars = new Parmars();
        parmars.setCustomerID(customerID);
        parmars.setToken(customerToken);
        String data = getRequestByParams(Constant.GETSHIPPINGMSGSETTINGBYCICID_URL,parmars);
        PushSettingsModel pushSettingsModel = JsonUtils.fromJson(data, PushSettingsModel.class);
        if (pushSettingsModel!= null) {
            if (pushSettingsModel.getData() != null) {
                return pushSettingsModel.getData();
            }
        }
        return null;
    }



    /**
     * 用户使用默认配置注册物流信息推送功能
     * @param customerID 用户登陆时的ID
     * @param customerToken 用户登陆的Token
     */
    public boolean registShippingPush(Long customerID, String customerToken) {

        Parmars parmars = new Parmars();
        parmars.setCustomerID(customerID);
        parmars.setToken(customerToken);
        try {
            JSONObject data = requestHttpPostData(Constant.REGISTSHIPPINGPUSH_URL, parmars);
            return data.optBoolean(Constant.JSON_ISSUCCESS);
        } catch (DxException e) {
            e.printStackTrace();
        }
        return false;
    }
}
